<?php
/**
 * 多进程执行任务
 * User: ahao
 * Email: 827316006@qq.com
 * Date: 2019/12/2 20:55
 */
echo "process -start-time:" . date('Ymd H:i:s') . PHP_EOL;
$workers = [];
$urls = [
    'http://baidu.com',
    'http://sina.com.cn',
    'http://qq.com',
    'http://baidu.com?search=ahao',
    'http://baidu.com?search=ahao2',
    'http://baidu.com?search=imooc',
    'http://baidu.com?search=imooc',
    'http://baidu.com?search=imooc',
    'http://baidu.com?search=imooc',
    'http://baidu.com?search=imooc',
];

for ($i = 0; $i < count($urls); $i++) {
    //子进程
    $process = new swoole_process(function (swoole_process $worker) use($i, $urls){
        //curl
        $content = curlData($urls[$i]);
        $worker->write($content . PHP_EOL);
    },true);
    $pid = $process->start();
    $workers[$pid] = $process;
}

foreach ($workers as $process) {
    echo $process->read();
}
/**
 * 模拟请求url内容
 * @param $url
 * @return string
 */
function curlData($url)
{
    //curl file_get_contents
    sleep(1);
    return $url . "：success" . PHP_EOL;
}

echo "process -end-time:" . date('Ymd H:i:s') . PHP_EOL;